package tools;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


public class InvitationCode {

	private static Map<Character, Integer> map =  new HashMap<Character, Integer>();;
//	
//	private static char[] arr = {'0','1','2','3','4','5','6','7','8','9',
//		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
//		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//	
	private static char[] arr = {'1','Z','f','X','p','M','t','7','K','h','W','9','m','3','N','6','j','P','I','v','k','u','o','U','V','r','q','c','O','4','R','b','l','G','H','g','T','E','J','D','w','z','Q','d','0','L','B','a','y','S','F','s','A','Y','i','2','n','C','x','e','8','5'};
	
	/*
	public static int random(int max) {
		return (int)(Math.random() * max);
	}
	
	public static void main(String[] args) {
		
		List<Character> list = new LinkedList(Arrays.asList('0','1','2','3','4','5','6','7','8','9',
				'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
				'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'));

		List newList = new ArrayList();
		
		while(list.size() > 0) {
			int index = random(list.size());
			char ch = list.get(index);
			System.out.print(",'" + ch + "'");
			list.remove(index);
		}
		
	}
	*/
	static{		
		for(int i=0;i<arr.length;i++){
			map.put(arr[i], i);
		}
	}
	public static long deCode(String s){
		char[] csOld = s.toCharArray();
		char[] cs = new char[csOld.length] ;
		int k = 0;
		for(int j=csOld.length-1;j>=0;j--){
			cs[k++] = csOld[j];
		}
		
		long sum = 0;		
		for(int i=0;i<cs.length;i++){
			sum += map.get(cs[i]) * Math.pow(62, i);
		}
		return sum;
	}
	
	
	public static String enCode(String s){
		
		long a = Long.parseLong(s);
		LinkedList list = new LinkedList();
		LinkedList listNew = calculate(a,list);
		char[] cs = new char[listNew.size()];
		int j=0;
		for(int i=listNew.size()-1;i>=0;i--){
			
			cs[j++] = (Character) listNew.get(i);
		}
		return new String(cs);		
	}
	
	
	
	private static LinkedList calculate(long n ,LinkedList list){
		//求商
		long i = n / arr.length ;
		//求余數
		long j = n % arr.length ;
		
		list.add(arr[(int) (j)]);
		
		if(i >= arr.length){
			calculate(i,list);
		}else{
			list.add(arr[(int) (i)]);
		}
		
		return list;
		
	}
}
